package com.wn.inventory.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.wn.common.PageBean;
import com.wn.entity.InventoryBookkeeping;
import com.wn.inventory.mapper.InventoryBookkeepingMapper;
import com.wn.inventory.service.InventoryBookkeepingService;
import com.wn.inventory.vo.InventoryBookkeepingVo;
import com.wn.utils.PageBeanUtil;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageDeliveryMode;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;

/**
 * @ author     ：NST
 * @ date       ：Created in 2022/10/19 15:34
 * @ description：库存台账service实现类
 * @ modified By：
 * @ version:
 */
@Service
public class InventoryBookkeepingImpl implements InventoryBookkeepingService {
    @Autowired
    RabbitTemplate rabbitTemplate;
    @Autowired
    InventoryBookkeepingMapper inventoryBookkeepingMapper;
    @Autowired
    RedisTemplate<String, Object> redisTemplate;
    /**
     * @param inventoryBookkeeping
     * @return void
     * @ author nst
     * @ date 2022/10/20 19:39
     * @ Description 添加库存台账 转入消息队列执行
     */
    @Override
    public void insert(InventoryBookkeeping inventoryBookkeeping) {
        inventoryBookkeeping.setState("待检验");
        rabbitTemplate.convertAndSend("exchangeBookkeeping", "bookkeeping", inventoryBookkeeping, new MessagePostProcessor() {
            @Override
            public Message postProcessMessage(Message message) throws AmqpException {
                //设置消息持久化
                message.getMessageProperties().setDeliveryMode(MessageDeliveryMode.PERSISTENT);
                return message;
            }
        });
    }
    /**
     * @param inventoryBookkeepingVo
     * @return com.wn.common.PageBean<com.wn.entity.InventoryBookkeeping>
     * @ author nst
     * @ date 2022/10/20 19:39
     * @ Description 多条件查询库存台账信息 先查redis缓存 没有再查数据库 并且放入缓存中
     */
    @Override
    public PageBean<InventoryBookkeeping> selectByCondition(InventoryBookkeepingVo inventoryBookkeepingVo) {
        Page<InventoryBookkeeping> page = PageHelper.startPage(inventoryBookkeepingVo.getCurrPage(), inventoryBookkeepingVo.getPageSize());
        List<InventoryBookkeeping> list = inventoryBookkeepingMapper.selectByCondition(inventoryBookkeepingVo.getInventoryBookkeeping());
        return PageBeanUtil.getPageBean(list, page, inventoryBookkeepingVo.getCurrPage(), inventoryBookkeepingVo.getPageSize());

    }
    /**
     * @param inventoryBookkeeping
     * @return void
     * @ author nst
     * @ date 2022/10/20 19:40
     * @ Description 修改库存台账信息 并删掉对应的库存台账缓存
     */
    @Override
    public void update(InventoryBookkeeping inventoryBookkeeping) {
        inventoryBookkeepingMapper.update(inventoryBookkeeping);
        HashOperations<String, Object, Object> hash = redisTemplate.opsForHash();
        hash.delete("inventory");
    }
}
